﻿using Word;
using Stylet;
using Stylet.Xaml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfSeasonUpload.Pages
{
    public class Page1ViewModel:Screen
    {
        public string Name { get; set; } = "hello, mvvm";
        public Document MyWord { get; set; }
        public void OpenWord()
        {
            var wordApp =new  Word.ApplicationClass();
            wordApp.Visible = true;
            this.MyWord= wordApp.Documents.Open(@"D:\练习源码\病历自动书写\病历自动生成\病历书写模板test.docx");
            
            ReplaceHeaderStr(wordApp, "$床号$", "dfjdkfjdf");
            ReplaceStr(wordApp, "$姓名$", "dfjdkfjdf");

        }
        public Range GetHeaderFooter(ApplicationClass app)
        {
          return  app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        }
        public void SetHeaderFooter(ApplicationClass app)
        {
            app.ActiveDocument.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "你好";
        }
        public void ReplaceHeaderStr(ApplicationClass app,string old,string newstr)
        {
            var find = GetHeaderFooter(app).Find;
            find.ClearFormatting();
            find.Replacement.ClearFormatting();
            find.Execute(FindText:old,ReplaceWith:newstr);

        }
        public void ReplaceStr(ApplicationClass app, string old, string newstr)
        {
            var find = app.Selection.Find;
            find.ClearFormatting();
            find.Replacement.ClearFormatting();
            find.Execute(FindText: old, ReplaceWith: newstr);

        }
    }
}
